package cn.heyige.backend.controller;

import cn.heyige.backend.common.CommResponse;
import cn.heyige.backend.service.MembershipService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("api/membership")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "会员服务", description = "会员信息、套餐管理、升级服务")
public class MembershipController {
    
    private final MembershipService membershipService;

    @GetMapping("/info")
    @Operation(summary = "获取会员信息", description = "获取用户当前会员状态和权限")
    @Parameter(name = "Authorization", description = "Bearer Token", required = true)
    public CommResponse<Map<String, Object>> getMembershipInfo(@RequestAttribute("userId") String userId) {
        try {
            Map<String, Object> response = membershipService.getMembershipInfo(userId);
            return CommResponse.ok(response);
        } catch (Exception e) {
            log.error("获取会员信息失败", e);
            return CommResponse.fail(500, "获取会员信息失败");
        }
    }
    
    @GetMapping("/plans")
    @Operation(summary = "获取会员套餐列表", description = "获取所有可用的会员套餐")
    public CommResponse<Map<String, Object>> getMembershipPlans() {
        try {
            Map<String, Object> response = membershipService.getMembershipPlans();
            return CommResponse.ok(response);
        } catch (Exception e) {
            log.error("获取会员套餐失败", e);
            return CommResponse.fail(500, "获取套餐信息失败");
        }
    }
}